package com.ruoyi.common.email;

import com.ruoyi.common.utils.VerifyCodeUtils;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class EmailVerificationUtil {

    public static boolean sendVerificationCode(String recipientEmail,String code) {
        // 发件人邮箱
        String senderEmail = "opendevelop@foxmail.com";
        // 发件人邮箱密码
        String senderPassword = "akngommmxjqzbdgg";
        // 收件人邮箱
        String toEmail = recipientEmail;
        // 邮件主题
        String subject = "验证码邮件";
        // 生成随机验证码
        String verificationCode = code;
        // 邮件内容
        String message = "【鸥睿科技】您的验证码是：" + verificationCode;

        // 设置邮件发送属性
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.qq.com"); // 你的 SMTP 服务器地址
        properties.put("mail.smtp.port", "587"); // SMTP 服务器端口号

        // 创建新的会话
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(senderEmail, senderPassword);
            }
        });

        try {
            // 创建消息对象
            Message emailMessage = new MimeMessage(session);
            emailMessage.setFrom(new InternetAddress(senderEmail));
            emailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            emailMessage.setSubject(subject);
            emailMessage.setText(message);

            // 发送邮件
            Transport.send(emailMessage);

            System.out.println("验证码已发送至邮箱：" + toEmail);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("发送邮件时出错：" + e.getMessage());
            return false;
        }
    }

    public static void main(String[] args) {
        // 使用示例
        String recipientEmail = "ls1006@126.com"; // 收件人邮箱地址
    }
}
